﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DWH.Utils;
using System.IO;
using System.Drawing;
using log4net;
using DWH.Data;
using System.Threading;

namespace DWH.PP.LoopUrls.CL
{
    /// <summary>
    /// http://c1520.biz.tm/index.php
    /// </summary>
    public class CLSavePageAttachmentTask : PPLoopUrlsBoardPullTask
    {
        private ILog logger;       

        public CLSavePageAttachmentTask()
        {
            logger = LogManager.GetLogger(typeof(CLSavePageAttachmentTask));
        }

        public override string ProcessTargetHtmlContent(string pageContentUrl, string targetHtmlContent)
        {
            if (!String.IsNullOrEmpty(attachmentStoreFolder))
            {
                string attFolderForThisPage = Path.Combine(attachmentStoreFolder, pageContentUrl.GetHashCode().ToString());
                //if (!Directory.Exists(attFolderForThisPage))
                //{
                //    Directory.CreateDirectory(attFolderForThisPage);
                //}
                //move download attachment to another task, skip the foder creation

                //only bfxzw torrent can be download so far, will investigate rmdown torrent
                List<string> torrentCans = DWHHelper.ExtractPatternFromStringContent(targetHtmlContent, DWHConstants.REGEX_HTML_CL_BFXZW, DWHConstants.REGEX_HTML_A_GROUP_LINKURL);

                foreach (string linkUrl in torrentCans)
                {
                    if (linkUrl.Contains(DWHConstants.FLAG_TORRENT_BFXZW))
                    {
                        logger.InfoFormat("push torrent to db for later download:{0}",linkUrl);

                        DWHDBHelper.AddUndownloadedPageAttachment(linkUrl, pageContentUrl.GetHashCode(),
                            DWHConstants.ATTACHMENT_TYPE_TORRENT_BFXZW,
                            //String.Format("Torrent-{0}", DWHConstants.FLAG_TORRENT_BFXZW), 
                            attFolderForThisPage, DateTime.Now);
                        
                        //try
                        //{
                        //    DWHHelper.DownloadTorrentCLBfxzw(linkUrl, attFolderForThisPage);
                        //}
                        //catch (Exception ex)
                        //{
                        //    logger.ErrorFormat("error occors when trying to download torrent from bfxzm.com, url:{0} err:{1}", linkUrl, ex.ToString());
                        //}
                        continue;
                    }

                    if (linkUrl.Contains(DWHConstants.FLAG_TORRENT_RMDOWN))
                    {
                        logger.InfoFormat("push torrent to db for later download:{0}", linkUrl);

                        DWHDBHelper.AddUndownloadedPageAttachment(linkUrl, pageContentUrl.GetHashCode(),
                            DWHConstants.ATTACHMENT_TYPE_TORRENT_RMDOWN,
                            //String.Format("Torrent-{0}", DWHConstants.FLAG_TORRENT_BFXZW),
                            attFolderForThisPage, DateTime.Now);

                        continue;
                    }
                }

                List<string> images = DWHHelper.ExtractPatternFromStringContent(targetHtmlContent,
                    DWHConstants.REGEX_HTML_IMG, DWHConstants.REGEX_HTML_IMG_GROUP_IMGURL);

                logger.InfoFormat("push image url to db for later download, image count:{0}", images.Count);

                DWHDBHelper.AddUndownloadedPageAttachments(images, pageContentUrl.GetHashCode(),
                    DWHConstants.ATTACHMENT_TYPE_IMAGE,      
                    //"Image",
                    attFolderForThisPage, DateTime.Now);

                //DWHHelper.DownloadWebPageImages(TaskName, attFolderForThisPage, images);   
            }          

            return targetHtmlContent;
        }
    }
}
